package android.medicalcloud.com.yiapp.yilian.reiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.PushTool;
import android.os.Bundle;
import android.util.Log;

import com.igexin.sdk.PushConsts;

import org.json.JSONObject;

/**
 * Created by wanggenlin on 17-5-31.
 */
public class MessageReiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {


        Bundle bundle = intent.getExtras();


        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传（payload）数据
                byte[] payload = bundle.getByteArray("payload");

                if (payload != null) {
                    try {
                        String   data = new String(payload);
                        Log.e("GexinSdkDemo", "onReceive() action=" + data);

                        JSONObject is = new JSONObject(data);
                        JSONObject display = is.getJSONObject("display");
                        JSONObject click = is.getJSONObject("click");

                        PushTool.sendNotification(JsonTool.getString(display.toString(),"title"),
                                JsonTool.getString(display.toString(),"content"),
                                JsonTool.getString(display.toString(),"ticker"),
                                JsonTool.getString(click.toString(),"type"),
                                JsonTool.getString(click.toString(),"extra"));
                    }catch (Exception e){
                        Logger.e(e.getLocalizedMessage(),e);
                    }

                }
                break;
            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                String cid = bundle.getString("clientid");
                Log.d("GexinSdkDemo", "Got ClientID:" + cid);
                // TODO:
                    /* 第三方应用需要将ClientID上传到第三方服务器，并且将当前用户帐号和ClientID进行关联，以便以后通过用户帐号查找ClientID进行消息推送
                    有些情况下ClientID可能会发生变化，为保证获取最新的ClientID，请应用程序在每次获取ClientID广播后，都能进行一次关联绑定 */

                break;
            default:
                break;

        }
    }
}
